async function injectAdminSidebar(activePath) {
  try {
    let res = await fetch('/static/admin/components/sidebar.html', { cache: 'no-cache' });
    if (!res.ok) {
      // 回退相对路径以兼容不同部署前缀
      res = await fetch('static/admin/components/sidebar.html', { cache: 'no-cache' });
      if (!res.ok) res = await fetch('./static/admin/components/sidebar.html', { cache: 'no-cache' });
      if (!res.ok) res = await fetch('./components/sidebar.html', { cache: 'no-cache' });
    }
    if (!res.ok) return;
    const html = await res.text();
    const container = document.querySelector('.admin-sidebar');
    if (!container) return;
    container.innerHTML = html;
    // 手风琴折叠：切换一级时收起其它
    const groups = container.querySelectorAll('.nav-group');
    groups.forEach(g => {
      const header = g.querySelector('.nav-group-header');
      const items = g.querySelector('.nav-group-items');
      if (!header || !items) return;
      header.style.cursor = 'pointer';
      header.addEventListener('click', () => {
        // 先收起所有
        groups.forEach(gg => {
          const ii = gg.querySelector('.nav-group-items');
          if (ii) ii.style.display = 'none';
        });
        // 展开当前
        items.style.display = 'block';
      });
      // 默认展开包含激活项的组
      items.style.display = 'none';
    });

    // 高亮当前项
    const current = activePath || location.pathname;
    const links = container.querySelectorAll('a.nav-item, a.nav-sub-item');
    let matched = false;
    links.forEach(a => {
      const href = a.getAttribute('href');
      if (href && current === href) {
        a.classList.add('active');
        // 展开命中的组
        const groupItems = a.closest('.nav-group-items');
        if (groupItems) groupItems.style.display = 'block';
        matched = true;
      }
    });
    // 若没有命中任何链接，默认展开第一个分组
    if (!matched) {
      const first = container.querySelector('.nav-group .nav-group-items');
      if (first) first.style.display = 'block';
    }
  } catch (e) {
    // 忽略错误
  }
}


